﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Twodi.Enums;
using Twodi.Aspect.Interfaces;

namespace Twodi.Camera
{
    public class WorldCamera<T> : Camera<T> where T : IBoundsAspect
    {
        protected Rectangle worldBounds;

        public WorldCamera(Rectangle viewPort, Rectangle worldBounds, Game game)
            : base(viewPort, game)
        {
            this.WorldBounds = worldBounds;
            this.Limits = worldBounds;

            this.LimitedProperties.TouchLimits += (sender, e) =>
            {
                switch (e.Exit)
                {
                    case Direction.North:
                        Position += new Vector2(0, Limits.Top - (this.Position.Y - this.Center.Y));
                        break;
                    case Direction.South:
                        Position -= new Vector2(0, (this.Position.Y + this.Center.Y) - Limits.Bottom);
                        break;
                    case Direction.East:
                        Position -= new Vector2((this.Position.X + this.Center.X) - Limits.Right, 0);
                        break;
                    case Direction.West:
                        Position += new Vector2(Limits.Left - (this.Position.X - this.Center.X), 0);
                        break;
                    default:
                        break;
                }
            };
        }

        public Rectangle WorldBounds
        {
            get { return this.worldBounds; }
            set
            {
                this.worldBounds = value;
                onWorldBoundsChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> WorldBoundsChanged;

        protected virtual void onWorldBoundsChanged(EventArgs e)
        {
            if (WorldBoundsChanged != null)
            {
                WorldBoundsChanged(this, e);
            }
        }
    }
}
